package com.ag.zlw.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

/**
 * 
 * TbRoleDAO扩展DAO
 * 
 * @author 黎肈明
 * 
 */

public class TbRoleDAO extends com.ag.dao.TbRoleDAO {
	private static final Log log = LogFactory.getLog(TbRoleDAO.class);

	@Autowired
	public void init(SessionFactory sessionFactory) {
		super.setSessionFactory(sessionFactory);
	}

	public List find(String queryString) {
		log.debug("finding all instances");
		try {
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find failed", re);
			throw re;
		}
	}

	public List find(String queryString, Object value) {
		log.debug("finding all instances");
		try {
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find failed", re);
			throw re;
		}
	}
	
	public List find(String queryString, Object[] value) {
		log.debug("finding all instances");
		try {
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find failed", re);
			throw re;
		}
	}

	/**
	 * Hibernate复杂用法
	 * 
	 * @param hsql
	 * @param firstRow
	 * @param maxRow
	 * @return
	 * @throws Exception
	 */
	public List find(final String hsql, final int firstRow, final int maxRow)
			throws Exception {
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException,
					SQLException {
				Query query = s.createQuery(hsql);
				// query.setFirstResult(firstRow);
				// query.setMaxResults(maxRow);
				List list = query.list();
				return list;
			}
		});
	}

	/**
	 * Hibernate复杂用法
	 * 
	 * @param hsql
	 * @param firstRow
	 * @param maxRow
	 * @return
	 * @throws Exception
	 */
	public List find1(final String hsql) throws Exception {
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException,
					SQLException {
				// s.delete(hsql);
				s.createQuery(hsql).executeUpdate();
				// Query query = s.createQuery(hsql);
				// s.d
				// query.setFirstResult(firstRow);
				// query.setMaxResults(maxRow);
				// List list = query.list();
				// return list;
				return null;
			}
		});
	}

	/**
	 * 执行数据更新Hibernate高级回调
	 * 
	 * @param hsql
	 * @return
	 * @throws Exception
	 */
	public Object execute(final String hsql) throws Exception {
		return getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException,
					SQLException {
				return s.createQuery(hsql).executeUpdate();
			}
		});
	}

}
